﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// This is somewhat similar to the monotony filter. 
    /// Each condition is checked to be fulfilled in a predefined neighbour environment
    /// (8 or 4 neighbours)
    /// If it is true, the dictionary assigned color is set, if not the background color is set.
    /// </summary>
    public class ConditionalMonotony : BaseForeBackgroundColorFilter
    {
        public delegate bool ConditionalMonotonyCondition(Color _actualColor, Color[] aColor);

        protected Dictionary<ConditionalMonotonyCondition, Color> m_aConditionColorAssignment = new Dictionary<ConditionalMonotonyCondition, Color>();

        /// <summary>
        /// Initializes a new instance of the <see cref="ConditionalMonotony"/> class.
        /// </summary>
        public ConditionalMonotony()
        {
            m_aConditionColorAssignment.Add(
                new ConditionalMonotonyCondition(
                    delegate(Color color, Color[] _aColor)
                    {
                        int nCount = 0;
                        foreach (Color colorTemp in _aColor)
                        {
                            if (ColorFunctions.DistanceSquared(color, colorTemp) < 1000)
                            {
                                nCount++;
                            }
                        }
                        return nCount <= 6;
                    }), Definitions.White);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ConditionalMonotony"/> class.
        /// </summary>
        /// <param name="_conditionalMonotony">The _conditional monotony.</param>
        internal ConditionalMonotony(ConditionalMonotony _conditionalMonotony)
        {
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            int nHeight = _bitmap.Height;
            int nWidth = _bitmap.Width;

            Color[,] aColorBitmap = _bitmap.GetAsPixelArrayColor();
            Color[] aColor = new Color[8];

            List<Point> aNeighbour = m_aNeighbour[m_NEIGHBOUR];

            for (int y = 0; y < nHeight; y++)
            {
                for (int x = 0; x < nWidth; x++)
                {
                    Color color = aColorBitmap[x, y];
                    aColor = Segmentation.GetEnvironmentPixelColorArray(aColorBitmap, x, y, aNeighbour, aColor);
                    Color colorToSet = Color.Transparent;
                    foreach (KeyValuePair<ConditionalMonotonyCondition, Color> keyValuePair in m_aConditionColorAssignment)
                    {
                        bool bResult = keyValuePair.Key(color, aColor);
                        colorToSet = bResult ? keyValuePair.Value : m_ColorBackground;
                        if (bResult) break;
                    }
                    _bitmap.SetPixel(x, y, colorToSet);
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new ConditionalMonotony(this);
        }
    }
}
